using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Net;
using System.Configuration;
using VRCPRServer.Models;
using System.Net.Sockets;
using UnityEngine;
using System.Text.RegularExpressions;
using VRCPRServer.NetWork;


public class ClientManager 
{
    public AsyncTcpClient tcpClient;
    private Thread thread;
    public DataBuffer dataBuffer;
    public Action SendHeart;
    /// <summary>
    /// 模拟人接状态
    /// </summary>
    public event Action<IPAddress[], int, bool> ServerConnetState;

    public ClientManager()
    {
        dataBuffer = new DataBuffer(20480, 9);
        NewClient();
        thread = new Thread(new ThreadStart(ThreadTimer));
        thread.IsBackground = true;
        thread.Start();
       
    }

    public void NewClient()
    {
        string serverAddress = "192.168.10.10";
        int serverPort = 8080;
        IPEndPoint localEP = new IPEndPoint(IPAddress.Parse(serverAddress),serverPort);
        tcpClient = new AsyncTcpClient(localEP);
        tcpClient.ServerConnected += TcpClient_ServerConnected;
        tcpClient.ServerDisConnected += TcpClient_ServerDisConnected;
        tcpClient.DatagramReceive += TcpClient_DatagramReceive;        
    }
    
    public void CreatClientSocketAndConnected()
    {
        tcpClient.Close();
        tcpClient.Connect();
    }

   

    /// <summary>
    /// 客户端和服务器断开连接事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void TcpClient_ServerDisConnected(object sender, TcpServerDisconnectedEventArgs e)
    {
        ServerConnetState?.Invoke(e.Addresses, e.Port, false);
    }


    /// <summary>
    /// 和服务器建立连接
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void TcpClient_ServerConnected(object sender, TcpServerConnectedEventArgs e)
    {
        ServerConnetState?.Invoke(e.Addresses, e.Port, true);
    }

    /// <summary>
    /// 收到服务器的数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void TcpClient_DatagramReceive(object sender, TcpDatagramReceivedEventArgs<byte[]> e)
    {
        try
        {
            if (e.Datagram == null || e.Datagram.Length < 1) return;

            string data = string.Empty;
            for (int i = 0; i < e.Datagram.Length; i++)
                data += e.Datagram[i] + " ";
            dataBuffer.WriteBuffer(e.Datagram);
        }
        catch (Exception ex)
        {
            Debug.Log(ex);
        }
    }
    #region 定时器

    private DateTime heartTime = DateTime.Now;

    private void ThreadTimer()
    {
        while (true)
        {
            if (Math.Abs(DateTime.Now.Second - heartTime.Second) >= 0.9)
            {
                heartTime = DateTime.Now;
                SendHeart?.Invoke();
                //SendToVR();
            }
          
            Thread.Sleep(1);
        }
    }

    #endregion

    /// <summary>
    /// 发送数据到
    /// </summary>
    private void SendToVR()
    {
        byte[] data = dataBuffer.ReadBuffer();
        if (data == null || data.Length != 7) return;
    }

    public void DisPose() {
        SendHeart = null ;
        tcpClient.Dispose();
        
    }


}
